package com.youdoneed.community.forum.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.youdoneed.base.application.AppException;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.forum.dao.ForumuserattentionanotherrelMapper;
import com.youdoneed.community.forum.entity.Forumuserattentionanotherrel;

/**
 * 用户相互关注服务接口
 *
 * @author lid
 * @date 2017.7.11
 */
@Service
public class ForumuserattentionanotherrelService {

    private final Logger logger = LoggerFactory.getLogger(ForumuserattentionanotherrelService.class);

    @Autowired
    ForumuserattentionanotherrelMapper forumuserattentionanotherrelMapper;

    /**
     * 批量加关注
     *
     * @param userId
     * @param friendIds
     * @author lid
     * @date 2017.12.26
     */
    public void saveAttentionInBatch(String userId, List<String> friendIds) {
        if (!StringUtils.hasText(userId)) {
            return;
        }

        if ((null == friendIds)
                || (0 == friendIds.size())) {
            return;
        }

        List<Forumuserattentionanotherrel> listRel = new ArrayList<>();
        Date createDate = new Date();
        for(int index = 0; index < friendIds.size(); index++){
            String friendId = friendIds.get(index);
            if(!StringUtils.hasText(friendId)){
                continue;
            }

            Forumuserattentionanotherrel rel1 = new Forumuserattentionanotherrel();
            Forumuserattentionanotherrel rel2 = new Forumuserattentionanotherrel();
            rel1.setUserId(userId);
            rel1.setAttentionedUserId(friendId);
            rel1.setCreateDate(createDate);
            rel1.setForumUserAttentionAnotherRelId(StringUUIdUtil.getUUID());
            listRel.add(rel1);
            rel2.setUserId(friendId);
            rel2.setAttentionedUserId(userId);
            rel2.setCreateDate(createDate);
            rel2.setForumUserAttentionAnotherRelId(StringUUIdUtil.getUUID());
            listRel.add(rel2);
        }
        logger.info("listRel.size():"+listRel.size());
        forumuserattentionanotherrelMapper.insertRecordBatch(listRel);
        logger.info("批量加入关注记录成功!");
    }

    /**
     * 获取我的关注用户id的数组
     *
     * @param userId
     * @return List<String>
     * @author lid
     * @date 2017.7.19
     */
    public List<String> getMyAttentionUsers(String userId) {
        if (!StringUtils.hasText(userId)) {
            logger.info("userId参数为空！");
            throw new AppException("userId参数不能为空！");
        }

        try {
            return forumuserattentionanotherrelMapper.getMyAttentionUsers(userId);
        } catch (Exception e) {
            logger.info("获取关注信息失败" + e);
            return null;
        }
    }

    /**
     * 获取所有关注我的用户id
     *
     * @param userId
     * @return
     */
    public List<String> getAttentionMeUserIds(String userId) {
        if (!StringUtils.hasText(userId)) {
            logger.info("userId参数为空！");
            throw new AppException("userId参数不能为空！");
        }

        try {
            return forumuserattentionanotherrelMapper.getAttentionMeUserIds(userId);
        } catch (Exception e) {
            logger.info("获取关注信息失败" + e);
            return null;
        }
    }

    /**
     * 保存关注信息
     *
     * @param rel
     * @return int
     * @author lid
     * @date 2017.7.11
     */
    @Transactional
    public int saveAttentionRel(Forumuserattentionanotherrel rel) {
        logger.info("saveAttentionRel");

        if (null == rel) {
            logger.info("null == rel");
            throw new AppException("null == rel");
        }

        if (!StringUtils.hasText(rel.getUserId())) {
            logger.info("userId参数为空！");
            throw new AppException("userId参数为空！");
        }

        if (!StringUtils.hasText(rel.getAttentionedUserId())) {
            logger.info("userId参数为空！");
            throw new AppException("被关注者userId参数为空！");
        }

        rel.setCreateDate(new Date());
        rel.setForumUserAttentionAnotherRelId(StringUUIdUtil.getUUID());
        try {
            return forumuserattentionanotherrelMapper.insert(rel);
        } catch (Exception e) {
            logger.info("保存关注信息失败" + e);
            return 0;
        }
    }

    /**
     * 计算我的关注者
     *
     * @param userId
     * @return
     */
    public int countMyFollower(String userId) {
        logger.info("countMyFollower");
        if (!StringUtils.hasText(userId)) {
            logger.info("userId参数为空！");
            throw new AppException("userId参数为空！");
        }

        try {
            return forumuserattentionanotherrelMapper.countMyFollower(userId);
        } catch (Exception e) {
            logger.info("获取关注信息失败" + e);
            return 0;
        }
    }

    public List<Forumuserattentionanotherrel> findAttention(Forumuserattentionanotherrel record, String currentPage, String rows) {
        try {
            int page = Integer.parseInt(currentPage);
            int row = Integer.parseInt(rows);
            PageHelper.startPage(page, row);
            return forumuserattentionanotherrelMapper.selectParam_page(record);
        } catch (Exception e) {
            logger.error("查询关注请求错误:" + e);
            return null;
        }
    }

    @Transactional
    public boolean save(Forumuserattentionanotherrel record) {
        if (null == record.getUserId() || "".equals(record.getUserId())) {
            throw new AppException("用户ID不能为空！");
        }
        if (null == record.getAttentionedUserId() || "".equals(record.getAttentionedUserId())) {
            throw new AppException("被关注用户ID不能为空！");
        }
        try {
            forumuserattentionanotherrelMapper.save(record);
            return true;
        } catch (Exception e) {
            logger.error("加关注请求错误:" + e);
            throw new AppException("加关注请求失败。");
        }
    }

    @Transactional
    public boolean delete(String id) {
        if (null == id) {
            throw new AppException("关注ID不能为空！");
        }
        try {
            forumuserattentionanotherrelMapper.deleteByPrimaryKey(id);
            return true;
        } catch (Exception e) {
            logger.error("取消关注请求错误:" + e);
            throw new AppException("取消关注请求失败。");
        }
    }

    public Forumuserattentionanotherrel findAttentionByUserIdFriendId(Forumuserattentionanotherrel record) {
        try {
            if (null == record.getUserId() || "".equals(record.getUserId())) {
                throw new AppException("用户ID不能为空！");
            }
            if (null == record.getAttentionedUserId() || "".equals(record.getAttentionedUserId())) {
                throw new AppException("被关注用户ID不能为空！");
            }
            return forumuserattentionanotherrelMapper.selectAttentionList(record);
        } catch (Exception e) {
            logger.error("查询是否关注过错误:" + e);
            throw new AppException("查询是否关注过错误失败。");
        }
    }

    public List<Forumuserattentionanotherrel> findRecommendAttention(String userId, Integer currentPage, Integer rows) {
        try {
            PageHelper.startPage(currentPage, rows);
            return forumuserattentionanotherrelMapper.selectRecommendAttention(userId);
        } catch (Exception e) {
            logger.error("查询推荐关注错误:" + e);
            throw new AppException("查询推荐关注失败。");
        }
    }

    public Integer isAttention(String userId, String attentionedUserId) {
        if (!StringUtils.hasText(userId)) {
            throw new AppException("userId为空");
        }

        if (!StringUtils.hasText(attentionedUserId)) {
            throw new AppException("attentionedUserId为空");
        }

        try {
            return forumuserattentionanotherrelMapper.isAttention(userId, attentionedUserId);
        } catch (Exception e) {
            logger.error("查询是否关注错误:" + e);
            throw new AppException("查询推荐关注失败。");
        }
    }

}
